package client.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;

import server.model.administration.MeetingService;
import server.model.meeting.Group;
import server.model.meeting.Participant;
import server.model.meeting.User;
import server.model.remotes.IMeetingService;


public class LoginGui extends JPanel {
	
	private GuiManager manager;
	private JLabel lblInfo;
	private JLabel lblName;
	private JLabel lblPassword;
	private JLabel lblFeedback;
	private JLabel lblPasswordForget;
	private JLabel lblPassText;
	
	private JTextField txtName;
	private JPasswordField txtPassword;
	private JTextField txtForgetPass;

	private JButton btnBack;
	private JButton btnLogin;	
	private JButton btnPass;
	private JButton btnSend;
	
	private SendEmail sendEmail;

	public LoginGui(GuiManager manager) {
		this.manager = manager;
		
		setSize(585, 365);
		setLocation(0, 0);
		setVisible(true);
		setLayout(null);

		initGUI();
	}

	private void initGUI() {

		lblInfo = new JLabel();
		lblInfo.setText("Log hieronder in met uw gegevens");
		lblInfo.setBounds(31, 12, 300, 20);
		lblInfo.setFont(new java.awt.Font("Segoe UI", 1, 14));
		lblName = new JLabel("Naam :");
		lblName.setBounds(31, 44, 100, 22);
		lblPassword = new JLabel("Wachtwoord :");
		lblPassword.setBounds(31, 72,  100, 22);
		lblFeedback = new JLabel();
		lblFeedback.setBounds(31, 100, 341, 22);
		lblFeedback.setForeground(Color.red);
		
		lblPassText = new JLabel("Vul hier uw emailadres in ");
		lblPassText.setBounds(31, 240, 150, 22);
		lblPassText.setVisible(false);
		
		lblPasswordForget = new JLabel();
		lblPasswordForget.setText("Wachtwoord vergeten?");
		lblPasswordForget.setBounds(31, 180, 300, 20);
		lblPasswordForget.setFont(new java.awt.Font("Segoe UI", 1, 14));
		
		txtName = new JTextField();
		txtName.setText("test");
		txtName.setBounds(131, 44, 118, 22);
		txtPassword = new JPasswordField();		
		txtPassword.setBounds(131, 72, 118, 22);
		
		txtForgetPass = new JTextField();
		txtForgetPass.setText("email@email.com");
		txtForgetPass.setBounds(31, 260, 150, 22);
		txtForgetPass.setVisible(false);

		btnLogin = new JButton("Inloggen");
		btnLogin.setBounds(129, 128, 120, 22);
		btnLogin.addActionListener(new btnLoginListener());
		btnBack = new JButton("Terug");
		btnBack.setBounds(31, 128, 70, 22);
		btnBack.addActionListener(new btnBackListener());
		btnPass = new JButton("Opvragen");
		btnPass.setBounds(31, 200, 120, 22);
		btnPass.addActionListener(new btnPassListener());
		
		btnSend = new JButton("Sturen");
		btnSend.setBounds(31, 290, 120, 22);
		btnSend.addActionListener(new btnSendEmailListener());
		btnSend.setVisible(false);
		
				
		add(lblInfo);
		add(lblName);
		add(lblPassword);
		add(lblPasswordForget);
		add(lblFeedback);
		add(lblPassText);
		
		add(txtName);
		add(txtPassword);
		add(txtForgetPass);

		add(btnLogin);		
		add(btnBack);
		add(btnPass);
		add(btnSend);
		
		repaint();
	}
	
	private class btnLoginListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			char[] chars = txtPassword.getPassword();
			String password = "";
			for (int i=0; i<chars.length; i++){
				password+=chars[i];
			}

			try {
				IMeetingService meetingService =  manager.getAdmin().loginUser(txtName.getText(), password);
				

				if (meetingService != null){
					manager.setMeetingService(meetingService);
					manager.switchScreen("menuGui");
					manager.setUserName(txtName.getText());
				}else{
					lblFeedback.setText("Uw gebruikersnaam of wachtwoord is onjuist.");
					
				}
				
				
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	private class btnBackListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("mainGui");
		}
		
	}
	
	private class btnPassListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			lblPassText.setVisible(true);
			txtForgetPass.setVisible(true);
			btnSend.setVisible(true);
		}
		
	}
	
	private class btnSendEmailListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
		MsgBox message = new MsgBox(null, "Mail verstuurd naar  " +  txtForgetPass.getText());
		message.setSize(200, 100);
		
		/* Mail versturen.. Werkt alleen niet op school wegens smtp adressen
		 *  
		 * sendEmail.postMail(txtForgetPass.getText(), "Uw wachtwoord voor het inloggen", "In dit bericht staat uw wachtwoord.", "Administrator@IH02.com");
		 * 
		 */
			

		}
		
	}
	
	
	
	public void reset(){
		lblFeedback.setText("");
	}
	
	
}
